﻿using System;
using BizControl.Dal;
using Csla.Data;

namespace BizControl.DalLinq
{
  public class DalLinqManager : IDalManager
  {
    private static string _typeMask = typeof(DalLinqManager).FullName.Replace("DalLinqManager", @"{0}");

    // TO DO: Use method injection and IoC
    public T GetProvider<T>() where T : class
    {
      var typeName = string.Format(_typeMask, typeof(T).Name.Substring(1)); // removing "I" from ITypeName
      var type = System.Type.GetType(typeName);
      if (type != null)
        return Activator.CreateInstance(type) as T;
      else
        throw new NotImplementedException(string.Format(Properties.Resources.TypeNotSupported, typeName));
    }

    public ContextManager<BusinessControl2DataContext> ConnectionManager { get; private set; }

    public DalLinqManager()
    {
      ConnectionManager = ContextManager<BusinessControl2DataContext>.GetManager(Database.BizControlDB);
    }

    public void Dispose()
    {
      ConnectionManager.Dispose();
      ConnectionManager = null;
    }
  }
}
